
import '../http_error.dart';

export '../http_error.dart';

/*
  使用示例：
    new HttpResponse((Map<String,dynamic> result){

    },(HTTPError error,String msg){

    },complete:(){});

  * */

class HttpResponse {

  //接口响应数据，类型处理为需要类型
  dynamic _responseOriginalData;
  static String Service_Logic_Fail_Message = "业务异常";

  HttpResponse(Function success,Function(HTTPError error,String message) fail,{Function complete}){
    this.success = success;
    this.fail = fail;
    this.complete = complete;
  }
  //HttpCallBack(this.onSuccessRet ,this.onFailRet);

  //处理响应
  responseData(){
    if(_responseOriginalData == null){
      return null;
    }
    return serviceLogicResponse(_responseOriginalData);
  }

  //原属数据处理
  serviceOriginalResponse(obj){
    return obj;
  }

  //业务数据处理
  serviceLogicResponse(obj){
    return obj;
  }

  //是否响应成功
  bool setResponseData(obj){
    if(isResponseSuccess(obj)){
      _responseOriginalData = serviceOriginalResponse(obj);
      return isServiceLogicResponseSuccess(_responseOriginalData);
    } else{
      return false;
    }
  }

  bool isResponseSuccess(obj){
    return true;
  }

  //响应成功，业务逻辑是否成功，默认成功，便于子类统一处理业务
  bool isServiceLogicResponseSuccess(obj){
    return true;
  }

  //业务数据失败提示信息
  String serviceLogicFailMessage(){
    return Service_Logic_Fail_Message;
  }

  //成功回调
  Function success;//(T result)
  //失败回调
  Function(HTTPError error,String message) fail;//(HTTPError error,String message)
  //完成回调
  Function complete;//(){}
}